/* ************************************************************************************************

	Copyright (c) 2005-2006, IGR
	All Rights Reserved.
	Licensed under the Apache License version 2.0 or above.
	For more information on licensing, see:
        http://www.apache.org/licenses/


 ************************************************************************************************
 Objet TREEVIEW
 VERSION:0.85
 DATE:16/03/2006
 ************************************************************************************************
Init parameter in objPar (object of parameter):
 Name:               treeview name
 top:           	 Top position (in pixel)
 left:               Left position (in pixel)             
 width:              Width 
 height:             height  
 parent              object parent
 rootNode            root node of the treeview
 boolSelActive:      Node style change when selected
 position            absolute or relative
 rootOpen            initial open status (true if open)
 withRoot            if we want a root node
 withT               true if you want a dashed line between the nodes
 draggableNode		 true if the nodes of the tree coulb be draggable
*************************************************************************************************/


// *************************************
// CONSTRUCTOR
// *************************************

rialto.widget.Tree=function(objPar){
      
    
    //base constructor
    objPar.type="treeview";
    this.base = rialto.widget.AbstractComponent;    
    this.base(objPar);
    
    var oThis=this;
    
    //Default value
    this.rootOpen = true;
    this.boolSelActive = true;                
    this.withRoot=true;
    this.withT=true;
	this.autoResizableH=false;  
    this.autoResizableW=false; 
    this.draggableNode=false;
    //set the value with the properties of the objPar object
    if (rialto.lang.isBoolean(objPar.boolSelActive)){
        this.boolSelActive = objPar.boolSelActive;        
    }                                        
    if (rialto.lang.isBoolean(objPar.withT)){
        this.withT = objPar.withT;        
    }
    if (rialto.lang.isBoolean(objPar.rootOpen)){
        this.rootOpen = objPar.rootOpen;        
    }
    if (rialto.lang.isBoolean(objPar.withRoot)){
        this.withRoot = objPar.withRoot;        
    }
    if (rialto.lang.isBoolean(objPar.autoResizableH)){
        this.autoResizableH = objPar.autoResizableH;        
    }  
    if (rialto.lang.isBoolean(objPar.autoResizableW)){
        this.autoResizableW = objPar.autoResizableW;        
    }  
    if (rialto.lang.isBoolean(objPar.draggableNode)){
        this.draggableNode = objPar.draggableNode;        
    }
    // *******************************************
	// GENERATION OF DOM ELEMENTS
	// *******************************************
        
    //FIRST DIV 
    this.divExt.style.top = this.top;
    this.divExt.style.left = this.left;
    
    
    //DIV representig the DD
	this.divDD=document.createElement("DIV");
	this.divDD.className="treeDivDD"; 
    
    this.divExt.style.className="Tree_DIVSUP";    
    this.divExt.style.position=this.position;	
    this.divExt.style.overflow='auto';
    this.divExt.id = this.name;

    
    //if root node
    if(objPar.rootNode){this.addRoot(objPar.rootNode)};
    //add to the parent
    if (objPar.parent) {this.placeIn(objPar.parent);};      
        
    //release object
	objPar=null;
}        
    
rialto.widget.Tree.prototype = new rialto.widget.AbstractComponent;




rialto.widget.Tree.prototype.release=function() {    
	if (this.rootNode.img2){this.rootNode.img2.onclick=null;}
	this.rootNode.remove();
}

rialto.widget.Tree.prototype.adaptToContext=function(parent) {   
    //INITIAL HEIGHT AND WIDTH
    if (this.autoResizableH){
        this.divExt.style.height=this.getNewParentHeight()-this.divExt.offsetTop;
    }
    else{
        this.divExt.style.height=this.height;
    }
    if (this.autoResizableW){
        this.divExt.style.width=this.getNewParentWidth()-this.divExt.offsetLeft;
    }
    else{
        this.divExt.style.width= this.width;
    }
    traceExec("this.divExt.style.height:"+this.divExt.style.height,1);
    traceExec("this.divExt.style.width:"+this.divExt.style.width,1);
}


//function to add a root node to the treeview    
rialto.widget.Tree.prototype.addRoot=function(rootNode){
    var oThis=this;
    
    this.rootNode=rootNode;
    //ROOT NODE kneed some change
    this.divExt.appendChild(this.rootNode.divExt);
    //save the root node on the tree object
    this.rootNode.refTree=this;
    //save on the root node that it is root
    this.rootNode.root=true;    
	//little change
    this.rootNode.divText.style.top="5px";
    //no img1 so decease the left of img2 and divText
    this.rootNode.img1.style.diplay="none";
    this.rootNode.img2.style.left="0px";     
    this.rootNode.divText.style.left="24px";        
    this.rootNode.DIVFILLE.style.left='0';
    //this.rootNode.DIVFILLE.style.width=this.rootNode.divExt.offsetWidth-this.rootNode.divExt.offsetLeft-2;
    //root node always open
    this.rootNode.DIVCONTENU.style.display='block';
    this.rootNode.open=true;
    this.rootNode.changeIcone();
    if (!this.withRoot){
        this.rootNode.divExt.removeChild(this.rootNode.DIVENTETE);        
    }
    else{
	    //if no rootnode we remove text an image
        this.rootNode.img1.style.display='none';
        this.rootNode.DIVT.style.display='none';
        this.rootNode.img2.onclick=function(e){oThis.rootNode.toggle();}            
    }               
}

rialto.widget.Tree.prototype.updateSize= function(){           
    if (this.autoResizableH){            
        this.updateHeight();        
    }   
    if(this.autoResizableW){        
        this.updateWidth();
    }      
}

rialto.widget.Tree.prototype.updateWidth=function(){  	
    //get new width of the parent
    var tailleCalc=parseInt(this.getNewParentWidth());
    this.divExt.style.width=tailleCalc-this.divExt.offsetLeft;             
}

rialto.widget.Tree.prototype.updateHeight=function(){        
    //get new height of the parent
    var tailleCalc=parseInt(this.getNewParentHeight());
    this.divExt.style.height=tailleCalc-this.divExt.offsetTop;                 
}




/*look if a node with id=nodeId exist in the child of node
return an object with:
	-propertie trouve that is a boolean indicating if the node has been find (true)
	-propertie node that is the first occurence find

*/
rialto.widget.Tree.prototype.exist = function (node,nodeId) {        
    stat={trouve:false,nd:null};  
    if (node.id==nodeId)
    {
        //c'est le noeud que l'on recherche
        stat={trouve:true,nd:node}
    }
    else{
        var i=0;       
        while (i<node.arrChildNode.length && ! stat.trouve)
        {            
            //on va regarder ses fils par reccursivit? 
            stat= this.exist(node.arrChildNode[i],nodeId); 
            i++;                     
       }            
    }
    return stat;
}

/*look if a node with associated value=val exist in the child of node
return an object with:
	-propertie trouve that is a boolean indicating if the node has been find (true)
	-propertie node that is the first occurence find

*/
rialto.widget.Tree.prototype.findValue = function (node,val) {           
    stat={trouve:false,nd:null};  
    if (node.val==val)
    {
        //c'est le noeud que l'on recherche
        stat={trouve:true,nd:node}
    }
    else{
        var i=0;       
        while (i<node.arrChildNode.length && ! stat.trouve)
        {            
            //on va regarder ses fils par reccursivit? 
            stat= this.findValue(node.arrChildNode[i],val); 
            i++;                     
       }            
    }
    return stat;
}

//to add a node to the treeview in the node with id=nodeIdPere
rialto.widget.Tree.prototype.addNode = function (node,nodeIdParent) { 
    
    //if id is the treeview id add a root node
    if(nodeIdParent==this.id){       
        this.addRoot(node);
    }
    else{        	
        //search the node parent to add this node
        var resRech=this.exist(this.rootNode,nodeIdParent);
        if(resRech.trouve){
        	resRech.nd.addNode(node);
        }
        else{
        	alert("not find "+node.text+" in "+nodeIdParent);
        }
    }
}


//will create a node and add it
rialto.widget.Tree.prototype.createAndAddNode=function(nodeId,objPar){              
    var node= new rialto.widget.TreeNode(objPar);
    this.addNode(node,nodeId);
    return node
}

/*

//to refresh a node after a loading
rialto.widget.Tree.prototype.refreshNode=function(nodeId,objPar){       
    //recupere le noeud pere
    var resRech=this.exist(this.rootNode,nodeId);
    if(resRech.trouve){
        var noeudPere=resRech.nd;   
        //on enleve le noeud loading du noeud pere
        noeudPere.removeNode(noeudPere.arrChildNode[0].id);
        //on ajoute le nouveau noeud au noeud pere
        var node= new rialto.widget.TreeNode(objPar);
        noeudPere.addNode(node);
    }
    else{alert("pas trouve "+node.text+" dans "+nodeIdParent);}    
}

*/

//to change style attribute
rialto.widget.Tree.prototype.setStyle=function(obStyle) {           
    for (prop in obStyle){                
        try {
            this.divExt.style[prop]=obStyle[prop];
            }
        catch (erreur) {}        
    }                                    
}

rialto.widget.Tree.prototype.getRoot=function(){
    //return the root node
    return this.rootNode;
}

rialto.widget.Tree.prototype.onclick = function (node) {
//The code must be redefine after the instanciation of the component. By default this method does nothing. 
//node is the one that has been clicked
}

/*
rialto.widget.Tree.prototype.onclickActionCommuneBefClic = function () {
//The code must be redefine after the instanciation of the component. By default this method does nothing. 
}
*/

//retourne une collection avec tous les noeuds qui ont le typeInfo en partant du nodeDep
rialto.widget.Tree.prototype.createCol=function (nodeDep,typeInfo) {                
    
    var collection=new Array();
    this.examineBranche(nodeDep,typeInfo,collection);
     
    collection.inCourCol=0;
    //on surchage la collection avec des methodes de navigation
    
    collection.isFirst=function(ind){
        return ind==0;
    }
    
    collection.isLast=function(ind){
        return ind==(this.length-1);
    }
    
    
    collection.clickFirst=function(){
        var node=this[0];
        node.click();
        this.inCourCol=0;
    }
    
    collection.clickLast=function(){
        var node=this[this.length-1];
        node.click();
        this.inCourCol=this.length-1;
    }
    
    collection.clickNext=function(){
        if(this.inCourCol!=this.length-1){
            var node=this[this.inCourCol+1];
            node.click();
            this.inCourCol+=1;
        }
    }
    
    collection.clickPrevious=function(){
        if(this.inCourCol!=0){
            var node=this[this.inCourCol-1];
            node.click();
            this.inCourCol-=1;
        }
    }
    
    return collection;
}    

//remplit la collection avec tous les noeuds qui ont le typeInfo en partant du root
rialto.widget.Tree.prototype.examineBranche=function (node,typeInfo,collection) {                    
    if (node.typeInfo==typeInfo){
        //c'est le noeud que l'on recherche
        collection.push(node);
    }    
    for (var i=0;i<node.arrChildNode.length;i++){            
        //on va regarder ses fils par reccursivit? 
        this.examineBranche(node.arrChildNode[i],typeInfo,collection);         
    }                    
}

/*

************************************************************************************************
 Objet TREENODE
 VERSION:0.85
 DATE:16/03/2006
 ************************************************************************************************
Init parameter in objPar (object of parameter):
 Name:               Name of the treenode
 sText:           	 text of the treenode
 sIcon:              path of an image (19*23) to symbolize the node  (src must be in rialtoEngine)  
 sIcon2:             path of an image (19*23) to symbolize the node when open (src must be in rialtoEngine) 
 onclick:            name of the function to call when clicking the node
 open                initial open status (true if open)
 reload            	 Node is reloadable using url attribute
 typeInfo            associated value
 url				 url called to get the new value of the node
*************************************************************************************************
	
	divExt
	 -------------------------------------------------------------------
	|	DIVENTETE														|
	|	 ---------------------------------------------------------		|
	|	| img1		img2		divText							  |		|
	|	| ------    ------	  ----------------------------------  |		|
	|	||		|  |	   | |									| |		|
	|	||		|  |	   | |									| |		|
	|	| ------    ------	  ----------------------------------  |		|			
	|	 ---------------------------------------------------------		|
	|	DIVCONTENU														|
	|	 ---------------------------------------------------------		|
	|	| DIVT		DIVFILLE									  |		|
	|	| ------    --------------------------------------------  |		|
	|	||		|  |	  									    | |		|
	|	||		|  |	  										| |		|
	|	| ------    --------------------------------------------  |		|			
	|	 ---------------------------------------------------------		|
  	 -------------------------------------------------------------------

*/
btwN = 0;
rialto.widget.TreeNode=function(objPar) {
		
    	//base constructor
	    objPar.type="treenode";
	    this.base = rialto.widget.AbstractComponent;    
	    this.base(objPar);

        var oThis=this;
                       
		//array of child node
        this.arrChildNode  = new Array();

		//Default value
		this.typeInfo = "node";
		this.text="node";
		this.icon = rialtoConfig.pathRialtoE + "images/imTreeview/pict_synthetik_off.gif";        
		this.icon2 = this.icon;
		this._onclick = "this.toggle();"
		this.open = true;
		this.reload=false;
		this.url="";
		this.srcPlus=rialtoConfig.pathRialtoE + "images/imTreeview/icT/L.gif";
        this.srcMoins=rialtoConfig.pathRialtoE + "images/imTreeview/icT/L.gif";
		this.draggable=false;
				
		
        //set the value with the properties of the objPar object    
	    if (rialto.lang.isString(objPar.typeInfo)){
	        this.typeInfo = objPar.typeInfo;        
	    }	    
	    if (rialto.lang.isString(objPar.sText)){
	        this.text = objPar.sText; 
	    }
	    if (rialto.lang.isString(objPar.sIcon,true)){
	        this.icon = rialtoConfig.pathRialtoE +objPar.sIcon;        
	        this.icon2 = this.icon;
	    } 
	    if (rialto.lang.isString(objPar.sIcon2,true)){
	        this.icon2 = rialtoConfig.pathRialtoE +objPar.sIcon2;        
	    } 
	    if (rialto.lang.isString(objPar.onclick,true)){
	        this._onclick = objPar.onclick;        
	    }
	    if (rialto.lang.isBoolean(objPar.open)){
	        this.open = objPar.open;        
	    }
	    if (rialto.lang.isBoolean(objPar.reload)){
	        this.reload = objPar.reload;        
	    }	
	    if (rialto.lang.isString(objPar.url,true)){
            this.url = objPar.url;        
        }     
                
        // *******************************************
		// GENERATION OF DOM ELEMENTS
		// ******************************************* 

        this.divExt.className="divTree";             
        //this.divExt.style.width='100%';
        
        //HEADER AND CONTENT OF THE NODE
        this.DIVENTETE=document.createElement("DIV");    
        this.DIVENTETE.style.height='23px';
        this.DIVENTETE.style.position='relative';
        this.DIVENTETE.style.overflow='hidden';
                         
        this.DIVCONTENU=document.createElement("DIV"); 
        this.DIVCONTENU.className="divTree";
        //IN IE Kneed that for that the DIVT with h=100% adapt his height when DIVCONTENU haight change
        if(rialto.config.userAgentIsIE){this.DIVCONTENU.style.height='0px'};        
        //image +/- to open or close              
        this.img1=document.createElement("DIV");
        this.img1.className="imgTreeview";
        this.DIVENTETE.appendChild(this.img1);                                            
        this.img1.onclick=function(e){            
            if (!e) var e = window.event;         
            //toggle the node         
            oThis.toggle();
            //cancel event
            stopEvent(e);
        }         
        //icon of the node 
        this.img2=document.createElement("DIV");         
        this.img2.style.backgroundImage="url('"+this.icon+"')";
        this.img2.className="imgTreeview";        
        this.img2.style.left=19;
        this.img2.onclick=function(){oThis.click();}        
        this.DIVENTETE.appendChild(this.img2);               
        //text of the node
        this.divText=document.createElement("DIV");         
        this.divText.className="DivText";        
        this.divText.style.left=40;
        this.divText.innerHTML=this.text;        
        this.divText.title=this.text;
        this.DIVENTETE.appendChild(this.divText);                   
        this.divText.onclick=function(){oThis.click();}
        //dashed line between the nodes
        this.DIVT=document.createElement("DIV");                         
        this.DIVT.style.display="none";        
        this.DIVT.style.position='absolute';                       
        this.DIVT.style.width='19px';        
        this.DIVT.style.height='100%';                
        this.DIVT.style.background="url('"+rialtoConfig.pathRialtoE+"images/imTreeview/icT/I.gif') repeat";
        this.DIVCONTENU.appendChild(this.DIVT);          
        //div that will content the child
        this.DIVFILLE=document.createElement("DIV");                                         
        this.DIVFILLE.style.left='19px';                 
        this.DIVFILLE.style.position='relative';        
        this.DIVFILLE.style.overflow='hidden';
        this.DIVFILLE.style.width='90%';        
        //this.DIVFILLE.style.height=0;
        this.DIVCONTENU.appendChild(this.DIVFILLE);        
        
        //display none because the div is style visible with ie
        this.DIVCONTENU.style.display='none';        
        
        this.divExt.appendChild(this.DIVENTETE);
        this.divExt.appendChild(this.DIVCONTENU);
        
        //RELOAD
        if (this.reload){	        
            this.noeudReload=new rialto.widget.TreeNode({sText:"Loading...",sIcon:"images/imTreeview/find.small.gif"});                        
            this.addNode(this.noeudReload);
        } 
       	        
        //release object
		objPar=null;         

}


rialto.widget.TreeNode.prototype = new rialto.widget.AbstractComponent;


rialto.widget.TreeNode.prototype.release= function () {
	this.img1.onclick=null;
	this.img2.onclick=null;
	this.divText.onclick=null;
	this.divText.oCiu=null;
	this.DIVENTETE.oCiu=null;	
	this.DIVENTETE.DDOuter=null;
	this.DIVENTETE.DDHover=null;	
	this.DIVENTETE.receiveAfterDrop=null;
	this.DIVENTETE.afterClic=null;
	oThis=null;
}


rialto.widget.TreeNode.prototype.addTargetBehavior=function() { 

		var oThis=this;

		//Add target behavior and event implementation		
		
		rialto.widgetBehavior.affect(this.DIVENTETE,'Target',{missileAsOnePixel:true,
		       												   domain:'tree.node'});		
		this.DIVENTETE.oCiu=this; 		       												   
		this.DIVENTETE.DDOuter = function ( missile) {            
            //hide the litte div
            oThis.refTree.divDD.style.display='none';
            oThis.divText.style.backgroundColor="";
        }
        
		this.DIVENTETE.DDHover = function ( missile) {            
			var top=missile.top;
			if(!this.oCiu.open){this.oCiu.toggle();}
            if(top>5 && top<18 && oThis.hasChild()){
                this.insert='add';
                oThis.refTree.divDD.style.display='none';
                //add the node
                oThis.divText.style.backgroundColor="#99CCFF";
            }
            else{
	            oThis.divText.style.backgroundColor="";
                if(top<5){
                    this.insert='before';
                    oThis.refTree.divDD.style.top=0;
                }
                else{
                    this.insert='after';
                    oThis.refTree.divDD.style.top=23;
                }
                oThis.refTree.divDD.style.display='block';
                //add the little div tha symbolize insert beetwen 2 node
                oThis.divExt.appendChild(oThis.refTree.divDD);
            }
        }    
        
        this.DIVENTETE.receiveAfterDrop = function (missile) {
            //hide the litte div
            oThis.refTree.divDD.style.display='none';
            //bug ie 
            if(rialto.config.userAgentIsIE){
    	        var bBugIe=false;
    	        //bugg if the next node has child	            
            	var nextNode=missile.oHtml.oCiu.next();
            	if(nextNode && nextNode.hasChild()){
	            	bBugIe=true;
            	}            	
            } 
            //can't drag in a child
            var resRech=oThis.refTree.exist(missile.oHtml.oCiu,oThis.id);            
            if(!resRech.trouve){                        
                //no change if the target is the same that the node we're moving
                if(this.oCiu!=missile.oHtml.oCiu){                                        
                    //add the node in the target
                    if(this.insert=='add'){                    
                        oThis.addNode(missile.oHtml.oCiu);
                    }
                    else{
                        //insert after or before the target
                        if(this.insert=='before'){
                            oThis.insertBefore(missile.oHtml.oCiu);
                        }
                        else{
                            oThis.insertAfter(missile.oHtml.oCiu);
                        }
                    }
                    if(rialto.config.userAgentIsIE && bBugIe){
	                   window.setTimeout("rialto.session.objects['"+nextNode.id+"'].setVisible(false);rialto.session.objects['"+nextNode.id+"'].setVisible(true)",100);
		            }                    
                    //Applicative Code
                    oThis.onDropIn(missile.oHtml.oCiu);
                }
            }
        }
}

rialto.widget.TreeNode.prototype.onDropIn=function(node) {     
//The code must be redefine after the instanciation of the component. By default this method does nothing. 
}

rialto.widget.TreeNode.prototype.addDDBehavior=function() {     

		var oThis=this;
		this.DIVENTETE.oCiu=this;
		this.addTargetBehavior();
		//Add d&d on the node
		rialto.widgetBehavior.affect(this.DIVENTETE,'Missile',
			{bMUpAction : false,
			bRectLim 	: false,	   							 
			 ghost		: {bIcone : true, aspect: 'icon'},
		   	 domainTargets:'tree.node'
		   	 ,targetChoice : 'firstIsBetter'
			});								
		
		        
        this.DIVENTETE.afterClic=function(e){            
            //deploie le noeud            
            oThis.click();         
        }; 

		this.bDD=true;
}


rialto.widget.TreeNode.prototype.reloadNode=function(url) {     
    // ADD in the url the tree id and the node id          
	var i =url.indexOf("?");    
    if (i==-1){
        url +='?TREEID='+this.refTree.id+'&NODEID='+this.id;
    }
    else{
        url +='&TREEID='+this.refTree.id+'&NODEID='+this.id;
    }                          
  	//Create the Request component
	this.remote=new rialto.io.AjaxRequest({
	url:url,
	callBackObjectOnSuccess:this,
	onSuccess: this.refreshReloadNode
	}) 
  	this.remote.load("");
    //remove reload attribute 
	this.reload=false;
	this._onclick="this.toggle();"           
	//If not open the node
	if (!this.open){this.toggle();}

}

//this function is an example of ajax refresh working with
//a JSON object {arrNode:[{objPar:{}},.....} where each objPar is a new node parameter object
//to make specific treatment the code must be redefine after the instanciation of the component.
rialto.widget.TreeNode.prototype.refreshReloadNode=function(request) {     
     try{     
     //remove loading node
     this.removeNode(this.noeudReload.id);
	 //retrieve a JSON object {arrNode:[{objPar:{}},.....} where each objPar is a new node
	 var colNode=eval("("+request.responseText+")");
	 for(var i=0;i<colNode.arrNode.length;i++){		
		var node= new rialto.widget.TreeNode(colNode.arrNode[i].objPar);
	    this.addNode(node);	 		 
	 }	 
	 }catch(e){alert(e)};	     
 }


rialto.widget.TreeNode.prototype.click = function () {
//--------------------------------------
//Rmq: les traitements des callbacks doivent correspondre ? des actions
// non sp?cifiques aux noeuds, sinon ils sont inutiles : ils
// pourraient ?tre incorpor? ds l'attribut action du noeud

// --> this.onclickActionCommuneBefClic et this.nclickApplicatif ont ils donc un sens ?
// this.refTree.onclickActionCommuneBefClic() et this.refTree.onclick
// ne suffisent ils pas ?
// ils permettent seult de d?finir un comportement (action) en dehors du constructeur de TreeNode
// il faudrait mieux utiliser une m?thode pour enrichir action de code pr? et post
// voir remplacer tte l'action; qq chose comme :
// rialto.widget.TreeNode.prototype.modAction(modAction,'before'|'after'|'action');

//== Traitements (callbacks) communs ? tous les noeuds d'1 arbre particulier 
//== ? r?aliser avant l' action li?e au noeud particulier
    //--- execute le code applicatif d?fini au niveau du noeud
   /* var oThis=this;
    var stopBefClick;
    stopBefClick = this.onclickActionCommuneBefClic();  

    //--- execute ?ventuellement le code applicatif d?fini au niveau de l'arbre
    if (!stopBefClick)
       this.refTree.onclickActionCommuneBefClic(this.typeInfo);       
*/
	//== select the node
    this.selNode();             

	//== execute the specific action
    eval(this._onclick);  
    

	//== Traitements (callbacks) communs ? tous les noeuds d'1 arbre particulier 
	//== ? r?aliser apres l' action li?e au noeud particulier
    //--- execute le code applicatif du noeud
    var stopClick;
    stopClick = this.onclick();  

    //--- execute ?ventuellement le code applicatif de l'arbre
    if (!stopClick)
       this.refTree.onclick(this);  
}

//function to open or close the node
rialto.widget.TreeNode.prototype.toggle = function () {    
    var oThis=this;            
    if (this.hasChild()){
        if (this.open){
            this.DIVCONTENU.style.display='none';                     
            this.open=false;            
        }
        else{  	                      
            this.DIVCONTENU.style.display='block';           
            this.open=true;
            //if it is a reload node the url is call
            if (this.reload){            	
                if(this.url){				   
                   this.reloadNode(this.url);
                }
                else{
                   eval(this.click);
                }
            }           
        }          
        this.changeIcone();
    }    
}

//Change the text of the node
rialto.widget.TreeNode.prototype.setText=function(sText) {     
    this.text=sText;
    this.divText.innerHTML=this.text;    
 }

//allow to sel a node without executing the associated action
rialto.widget.TreeNode.prototype.selNode = function () {   
    //I node could be sel (tree propertie)
    if (this.refTree.boolSelActive){         
        //unselect the current select node if exist
        if(this.refTree.currentSelNode!=null && this.refTree.currentSelNode!=this){
            this.refTree.currentSelNode.deSelNode();
        }
        //save the node as new current select node
        this.refTree.currentSelNode=this;      
        //change the text style
        if (this.hasChild()){
            //aspect de dossier                
            this.divText.className+=" NodeSel";
        }
        else{
            //aspect de feuille
            this.divText.className+=" textSel";
        }
    }
}

//allow to unsel a node
rialto.widget.TreeNode.prototype.deSelNode = function () {   
   if (this.refTree.boolSelActive){
        //change the text style
        this.divText.className="DivText";        
        if(this.refTree.currentSelNode!=null && this.refTree.currentSelNode==this){
            this.refTree.currentSelNode=null;
        }
   }
}


//return true if node has child
rialto.widget.TreeNode.prototype.hasChild = function () {
    if (this.arrChildNode.length==0){return false;}
    else{return true;}       
}

/*look if a node with associated id=nodeID exist in the child of this node
return index in arrChildNode if exist or -1
*/
rialto.widget.TreeNode.prototype.existChild = function (nodeID) {
    trouve=false;
    i=0;
    ind=-1;
    while (!trouve && i<this.arrChildNode.length){
        var node=this.arrChildNode[i];
        if (node.id==nodeID){
            trouve=true;
            ind=i;
        }
        i+=1;
    }
    return ind;
}




//to remove a node with id from the child nodes
//parameter: id =id of the node to remove
//bjustHTML:true if we want to remove only HTML elment

rialto.widget.TreeNode.prototype.baseRemove =  rialto.widget.AbstractComponent.prototype.remove;

rialto.widget.TreeNode.prototype.remove= function (bFromContainer) {	
	//Specific remove ---->remove of the chil node
	for(var i=0;i<this.arrChildNode.length;i++){
        this.arrChildNode[i].remove(bFromContainer);
    }
    //Call inherit method
	this.baseRemove(bFromContainer);
}


rialto.widget.TreeNode.prototype.removeNode = function (id,bjustHTML) {

    var ind=this.existChild(id);                
    if (ind!=-1){        
        var node=this.arrChildNode.splice(ind,1)[0];
        var decal=node.divExt.offsetHeight;
        if(node==this.refTree.currentSelNode){
        	node.deSelNode();
        }
        //remove           
        if(bjustHTML){
	        this.DIVFILLE.removeChild(node.divExt);
	    }
	    else{
	        node.remove();
	    }
	    
	    
	    
	   /* var parent=this;		
		while(parent){
			//alert("node:"+this.text+" decal:"+decal+" parent.text"+parent.text+" parent.DIVFILLE.offsetHeight:"+parseInt(parent.DIVFILLE.style.height));
		    parent.DIVFILLE.style.height=parseInt(parent.DIVFILLE.style.height)-decal; 
		    parent=parent.fatherNode;
	    }*/
	    //this.DIVFILLE.style.height=this.DIVFILLE.offsetHeight-decal;
        //change the icons
	    if(this.hasChild()){
	    	//may kneed to change the plus/minus of the last node (if the one we remove was the last)
	    	var lastNode=this.last();
	    	lastNode.changeIcone();
	    }
	    else{
	    	//add a plus/minus icon
	    	this.changeIcone();
	    }        	    
    }
}

rialto.widget.TreeNode.prototype.updateParentH = function (decal) {
	var parent=this.fatherNode;
	while(parent){
		//alert("node:"+this.text+" decal:"+decal+" parent.text"+parent.text+" parent.DIVFILLE.offsetHeight:"+parseInt(parent.DIVFILLE.style.height));
	    parent.DIVFILLE.style.height=parseInt(parent.DIVFILLE.style.height)+decal; 
	    parent=parent.fatherNode;
    } 
}

//function that will perform change on an node and his child after adding in the tree
rialto.widget.TreeNode.prototype.afterAdd = function () { 
    ///if it is not the same treeview
    this.refTree=this.fatherNode.refTree;
        
    //if(this.divExt.offsetWidth-19>0){
    //this.DIVFILLE.style.width=this.divExt.offsetWidth-19;
	//traceExec("node:"+this.text+" this.divExt.offsetWidth:"+this.divExt.offsetWidth+" this.divExt.offsetLeft"+this.divExt.offsetLeft,1);
//	alert("node:"+this.text+" this.divExt.offsetWidth:"+this.divExt.offsetWidth+" this.divExt.offsetLeft"+this.divExt.offsetLeft);
	
	/*var decal=this.divExt.offsetHeight;
	while(parent){
		//alert("node:"+this.text+" decal:"+decal+" parent.text"+parent.text+" parent.DIVFILLE.offsetHeight:"+parseInt(parent.DIVFILLE.style.height));
	    parent.DIVFILLE.style.height=parseInt(parent.DIVFILLE.style.height)+decal; 
	    parent=parent.fatherNode;
    } */   
    
    //D&D
    if(this.refTree.draggableNode && !this.bDD){
    	this.addDDBehavior();
    }
    //change the icons    
    this.changeIcone();
    //apply change to the child
    for(var i=0;i<this.arrChildNode.length;i++){
        var node=this.arrChildNode[i];
        node.afterAdd();
    }
}

//add a node 
rialto.widget.TreeNode.prototype.addNode = function (node) {    
   
    //if the node was in in another one we remove it from
    if (node.fatherNode){
        node.fatherNode.removeNode(node.id,true);        
    }
       
    //reference to the node in the child
    node.fatherNode = this;     
    //add to the array of child node
    this.arrChildNode.push(node);     
    //ADD the HTML element of the node    
    this.DIVFILLE.appendChild(node.divExt);
    if (this.open){
        //this.img1.style.background="url('"+this.srcMoins+"')";
    	this.DIVCONTENU.style.display='block';
    }
    else{
	    //this.img1.style.background="url('"+this.srcPlus+"')";
        this.DIVCONTENU.style.display='none';        
    }        
    //if the node is in a tree wa can call the after add method of the node
    if(this.refTree){        	    
        node.afterAdd();
        if(this.hasChild() && this.arrChildNode.length>1){
    		//change the plus/minus of the node before
    		var beforelastNode=this.last().previous();    	    	
    		beforelastNode.changeIcone();    	
    	}
	    else{
	    	//add a plus/minus icon	    	
	   		this.changeIcone();
	    }
    }
    
}

// function to insert a node before
rialto.widget.TreeNode.prototype.insertBefore = function (node) { 
    //if the node was in in another one we remove it from
    if (node.fatherNode){node.fatherNode.removeNode(node.id,true);}            
    //reference to the node in the child
    node.fatherNode = this.fatherNode;
  
    //get the index of this node
    ind=this.getIndex();;
    if (ind!=-1){
        //update the father node child array
        rialto.array.insert(this.fatherNode.arrChildNode,ind,node);
        //insert the html element of the node              
        this.fatherNode.DIVFILLE.insertBefore(node.divExt,this.divExt);      
    }
    //if the father node is in a tree wa can call the after add method of the node
    if(this.fatherNode.refTree){        	    
        node.afterAdd();        
    }   
}


//To insert the a node after
rialto.widget.TreeNode.prototype.insertAfter = function (node) { 
    
    if(this.isLast()){
        this.fatherNode.addNode(node);
    }
    else{                
        //next
        var nodeP=this.next();
        //if not the same that the one we want to insert
        if(nodeP!=node){
            nodeP.insertBefore(node);
        }        
    }
}

//this function update the icon of the node according to his position in the parent,his number of child...
rialto.widget.TreeNode.prototype.changeIcone = function () {
    //get the correct icon
    this.DIVT.style.display='none';
    if (!this.refTree.withT){
        if (this.hasChild()){           
            this.srcPlus=rialtoConfig.pathRialtoE + "images/imTreeview/icT/plus.gif";
	        this.srcMoins=rialtoConfig.pathRialtoE + "images/imTreeview/icT/minus.gif";
        }
    }    
    else{    
	    if(this.isLast() || (this.isLast() && this.isFirst())){
	    	//L icon
	    	if (this.hasChild()){
	        	this.srcPlus=rialtoConfig.pathRialtoE + "images/imTreeview/icT/Lplus.gif";
	        	this.srcMoins=rialtoConfig.pathRialtoE + "images/imTreeview/icT/Lminus.gif";            
		    }
		    else{
		        this.srcPlus=rialtoConfig.pathRialtoE + "images/imTreeview/icT/L.gif";
		        this.srcMoins=rialtoConfig.pathRialtoE + "images/imTreeview/icT/L.gif";            
		    }
	    }
	    else{
	    	//T icon
	    	if (this.hasChild()){
	            this.srcPlus=rialtoConfig.pathRialtoE + "images/imTreeview/icT/Tplus.gif";
	            this.srcMoins=rialtoConfig.pathRialtoE + "images/imTreeview/icT/Tminus.gif";            
	            this.DIVT.style.display='block';
	        }
	        else{
	            this.srcPlus=rialtoConfig.pathRialtoE + "images/imTreeview/icT/T.gif";
	            this.srcMoins=rialtoConfig.pathRialtoE + "images/imTreeview/icT/T.gif";            
	        }
	    }
	}	
    //Change the icon reference
    if (this.open){              
        this.img1.style.background="url('"+this.srcMoins+"')";
		this.img2.style.background="url('"+this.icon2+"')";
    }
    else{
        this.img1.style.background="url('"+this.srcPlus+"')";
        this.img2.style.background="url('"+this.icon+"')";

    }
    //hide img1 if no child and !refTree.withT
    if (!this.refTree.withT){
        if (this.hasChild()){
            this.img1.style.visibility='visible';
        }
        else{
            this.img1.style.visibility='hidden';
        }
    }
    else{
	    this.img1.style.visibility='visible';
    }    
}

//true if the node is the last son of his parent
rialto.widget.TreeNode.prototype.isLast = function () {         
   if (!this.root){
        return (this.fatherNode.arrChildNode[this.fatherNode.arrChildNode.length-1]==this);
    }
    else{return true}; 

}


//true if the node is the first son of his parent
rialto.widget.TreeNode.prototype.getIndex = function () {         
   return  rialto.array.indexOf(this.fatherNode.arrChildNode,this);
}


//true if the node is the first son of his parent
rialto.widget.TreeNode.prototype.isFirst = function () {         
    if (! this.root){
        return (this.fatherNode.arrChildNode[0]==this);
    }
    else{return true}; 
}


//return the first son of the node
rialto.widget.TreeNode.prototype.first = function () {         
    if (this.hasChild()){
        return (this.arrChildNode[0]);
    }
    else{return null}; 
}

//return the last son of the node
rialto.widget.TreeNode.prototype.last = function () {         
    if (this.hasChild()){
        return (this.arrChildNode[this.arrChildNode.length-1]);
    }
    else{return null}; 
}

//return next sibling of this node (or null if not)
rialto.widget.TreeNode.prototype.next = function () {         
    if (! this.root){    
        //index du noeud dans la collection de fils du pere
        var index=this.getIndex();
        //si ce n'est pas le dernier
        if (index<this.fatherNode.arrChildNode.length-1){
            return this.fatherNode.arrChildNode[index+1];
        }
        else{return null};
    }
    else{return null};
}

//return previous sibling of this node (or null if not)
rialto.widget.TreeNode.prototype.previous = function () {         
    if (! this.root){    
        //index du noeud dans la collection de fils du pere
        var index=this.getIndex();
        //si ce n'est pas le premier
        if (index>0){            
            return this.fatherNode.arrChildNode[index-1];
        }
        else{return null};       
    }
    else{return null};
}



rialto.widget.TreeNode.prototype.onclick = function () {
//The code must be redefine after the instanciation of the component. By default this method does nothing. 
}
rialto.widget.TreeNode.prototype.onclickActionCommuneBefClic = function () {
//The code must be redefine after the instanciation of the component. By default this method does nothing. 
}

//to change style attribute
rialto.widget.TreeNode.prototype.setStyle=function(obStyle) {       
    //on rajoute les anciennes prop qui ne sont pas red?finit dans le nouveau style
    for (prop in obStyle){                
        try {
            //eval("this.divText.style."+prop+"=obStyle."+prop+";");
            this.divText.style[prop]=obStyle[prop];
            }
        catch (erreur) {}        
    }                                    
}



